import requests
from threading import Thread
from multiprocessing import Process
import time

result = []

def request_sougou(index):
    time.sleep(1)
    url = "https://www.sogou.com/"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 Edg/87.0.664.75"
    }
    response = requests.get(url=url, headers=headers)
    print(f"序列号：{index}, 结果状态码：{response.status_code}")
    result.append(response)



# 多线程部分：
if __name__ == "__main__":
    start_time = time.time()
    thread_array = []
    for i in range(5):
        t = Thread(target=request_sougou, args=(i, ))
        thread_array.append(t)
        t.start()
    for t in thread_array:
        t.join()
    end_time = time.time()
    print("done!")
    print(result)
    print(f"多线程执行时间为:{end_time-start_time} s")


# 多进程部分：
if __name__ == "__main__":
    start_time = time.time()
    process_array = []
    for i in range(5):
        p = Process(target=request_sougou, args=(i, ))
        process_array.append(p)
        p.start()
    # 调用thread对象join接口，等任务完成后回到主进程
    for p in process_array:
        p.join()
    end_time = time.time()
    print("done!")
    print("多进程无法通过全局变量拿到返回结果")
    print(f"多进程执行时间为:{end_time-start_time} s")